package com.example.myapplication.anim;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.interpolator.view.animation.FastOutLinearInInterpolator;


import com.example.myapplication.R;
import com.example.myapplication.anim.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

public class AppCleanView extends LinearLayout {
    private List<Drawable> mList = new ArrayList<>();
    private onAnimationListener monAnimationListener;

    private int listSize = 0;
    private ImageView iv1, iv2, iv3, iv4;
    private LinearLayout layout;
    private TextView tv_app_number;
    private float sAnim1 = 0, eAnim1 = 0, sAnim2 = 0, eAnim2 = 0, sLAnim = 0, eLAnim = 0;
    private ObjectAnimator mAnimator1, mAnimator2, mAnimator3, mAnimator4;
    private AnimatorSet mAnimSet;
    private int mIndex1 = 0, mIndex2 = 1, mIndex3 = 2, mIndex4 = 3;

    public AppCleanView(Context context) {
        super(context);
        init(context);
    }

    public AppCleanView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AppCleanView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    //加载drawable数据
    public void initData(List<Drawable> mList) {
        this.mList = mList;
        listSize = mList.size();
        Log.i("llll", "initData: " + listSize);
        //超出范围则置为空
        iv1.setImageDrawable(listSize >= 1 ? mList.get(0) : null);
        iv2.setImageDrawable(listSize >= 2 ? mList.get(1) : null);
        iv3.setImageDrawable(listSize >= 3 ? mList.get(2) : null);
        iv4.setImageDrawable(listSize >= 4 ? mList.get(3) : null);
        tv_app_number.setText("0" + "/" + listSize);
        monAnimationListener.onDataCreated();
    }


    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_app_clean_anim, this);
        iv1 = findViewById(R.id.iv1);
        iv2 = findViewById(R.id.iv2);
        iv3 = findViewById(R.id.iv3);
        iv4 = findViewById(R.id.iv4);
        tv_app_number = findViewById(R.id.tv_app_number);
        layout = findViewById(R.id.layout);
        if (getResources().getConfiguration().locale.getLanguage().equals("ar")) {//如果是阿拉伯语，则更换动画方向
            sAnim1 = 0.f;
            eAnim1 = -ScreenUtils.getScreenSize(getContext())[0];
            sAnim2 = 0.f;
            eAnim2 = ScreenUtils.dpToPx(context, -65);
            sLAnim = 800.f;
            eLAnim = 0.f;
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv4.getLayoutParams();
            lp.leftMargin = 0;
            lp.rightMargin = ScreenUtils.dpToPx(context, -65);
            iv4.setLayoutParams(lp);
        } else {
            sAnim1 = 0.f;
            eAnim1 = ScreenUtils.getScreenSize(getContext())[0];
            sAnim2 = 0.f;
            eAnim2 = ScreenUtils.dpToPx(context, 65);
            sLAnim = -800.f;
            eLAnim = 0.f;
        }

        initAnimator();
    }

    //加载布局动画
    public void startLayoutAnimation() {
        setLayoutAnimation();
    }

    //加载子控件动画
    public void startItemAnimators() {
        mAnimSet.playTogether(mAnimator1, mAnimator2, mAnimator3, mAnimator4);
        mAnimSet.start();
        mAnimSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mIndex1++;
                mIndex2++;
                mIndex3++;
                mIndex4++;
                tv_app_number.setText(mIndex1 + "/" + listSize);

                if (mIndex4 >= listSize) {
                    iv4.setVisibility(INVISIBLE);
                } else {
                    iv4.setImageDrawable(mList.get(mIndex4));
                }

                if (mIndex3 >= listSize) {
                    iv3.setVisibility(INVISIBLE);
                } else {
                    iv3.setImageDrawable(mList.get(mIndex3));
                }

                if (mIndex2 >= listSize) {
                    iv2.setVisibility(INVISIBLE);
                } else {
                    iv2.setImageDrawable(mList.get(mIndex2));
                }

                if (mIndex1 >= listSize) {
                    iv1.setVisibility(INVISIBLE);
                } else {
                    iv1.setImageDrawable(mList.get(mIndex1));
                }

                if (mIndex1 >= listSize){
                    monAnimationListener.onAnimationOver();
                } else {
                    mAnimSet.start();
                }

            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
    }

    //给image view添加动画(imageview图像资源若为空，则直接将其设置为不可见并返回)

    private void initAnimator() {
        mAnimSet = new AnimatorSet();

        mAnimator1 = ObjectAnimator.ofFloat(iv1, "translationX", sAnim1, eAnim1);
        mAnimator1.setInterpolator(new FastOutLinearInInterpolator());


        mAnimator2 = ObjectAnimator.ofFloat(iv2, "translationX", sAnim2, eAnim2);
        mAnimator2.setInterpolator(new AnticipateOvershootInterpolator());


        mAnimator3 = ObjectAnimator.ofFloat(iv3, "translationX", sAnim2, eAnim2);
        mAnimator3.setInterpolator(new AnticipateOvershootInterpolator());


        mAnimator4 = ObjectAnimator.ofFloat(iv4, "translationX", sAnim2, eAnim2);
        mAnimator4.setInterpolator(new AnticipateOvershootInterpolator());
        ;

        mAnimSet.setDuration(400);
        mAnimSet.setStartDelay(150);


    }


    //平移动画
    private void setLayoutAnimation() {
        TranslateAnimation animation = new TranslateAnimation(sLAnim, eLAnim, 0, 0);
        animation.setDuration(500);
        layout.setAnimation(animation);
        animation.start();
    }


    public void setOnAnimationListener(AppCleanView.onAnimationListener onAnimationListener) {
        this.monAnimationListener = onAnimationListener;
    }

    public void destroy() {
        mAnimSet.end();
        mAnimSet.cancel();
    }


    public interface onAnimationListener {
        void onDataCreated();//资源加载完成后调用

        void onAnimationOver();//代表动画结束
    }
}
